<?php
/**
 * Created by PhpStorm.
 * User: lzhy@lzhy.me
 * Date: 2019/4/12 0012
 * Time: 15:24
 */
namespace app\admin\logic;

use app\admin\event\Utils;
use think\Db;
use think\facade\Request;

class Menu
{
    public static function menu($user)
    {
        $list = Db::table('menu')
            ->field('id,title,icon,path,role,pid')
            ->where('is_delete','=',0)
            ->cache(10)
            ->order('sort')
            ->select();

        $list = Utils::arr2tree($list);
        $userRole = array_filter(explode(',',$user['role']));
        list($dom,$activeMenuName) = self::_buildMenuDom($list,$userRole);

        return ['menu'=>$dom,'title'=>$activeMenuName];
    }

    /**
     * 生成菜单dom
     * @param $list
     * @param $role
     * @param int $level
     * @return array
     */
    private static function _buildMenuDom($list,$role,$level = 0)
    {
        $dom = '';
        $url = Request::url();
        $activeMenuName = '';
        foreach($list as $v){
            $currentNodeRole = array_filter(explode(',',$v['role']));
            if($currentNodeRole && !array_intersect($currentNodeRole,$role)){
                continue;
            }
            $_hasChild = (isset($v['children']) && $v['children']);
            $href = $v['path']?url($v['path']):'#';
            $icon = '<i class="fa fa-circle-o"></i>';
            if($v['icon']){
                $icon = '<i class="fa '.$v['icon'].'"></i>';
            }
            $active = '';
            if($url === $href){
                $active = 'active';
                $activeMenuName = $v['title'];
            }
            if(empty($active) && $_hasChild){
                $childHrefArr = array_map(function($v){
                    return url($v);
                },array_column($v['children'],'path'));
                if(in_array($url,$childHrefArr)){
                    $active = 'active';
                }
            }
            $rightArrow = '';
            $itemBase = '<li class="%s"><a href="%s">%s%s%s</a>';
            if($level == 0){
                $itemBase = '<li class="%s"><a href="%s">%s<span class="nav-label">%s</span>%s</a>';
            }
            if($_hasChild){
                $rightArrow = '<span class="fa arrow"></span>';
                $itemBase = '<li class="%s"><a href="%s">%s<span class="nav-label">%s</span>%s</a>';
                $href = '#';
            }
            $dom .= sprintf($itemBase,$active,$href,$icon,$v['title'],$rightArrow);
            if($_hasChild){
                if($level){
                    $dom .= '<ul class="nav nav-third-level">';
                }else{
                    $dom .= '<ul class="nav nav-second-level collapse">';
                }
                list($_dom,$_activeMenuName) = self::_buildMenuDom($v['children'],$role,$level+1);
                $dom .= $_dom;
                if($_activeMenuName){
                    $activeMenuName = $_activeMenuName;
                }
                $dom .= '</ul>';
            }
            $dom .= '</li>';
        }

        return [$dom,$activeMenuName];
    }
}